package com.oracle.lingshangejia.activity;

import android.app.Dialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.ConsultantsDialogAdapter;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.bean.ConsulationDoctorBean;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.pickerview.TimePickerView;
import com.oracle.lingshangejia.pickerview.Util;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.HashMap;

/**
 * 会诊
 */
public class ConsultationActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_title;
    private EditText et_yy;//会诊原因
    private EditText et_yj;//会诊意见
    private RelativeLayout rl_hz_doctor;//会诊医生
    private TextView tv_show_doctor;//显示医生
    private TextView tv_huizhen_date;//会诊日期
    private RelativeLayout rl_hz_ok;
    private int jmid;
    private int jzid;
    private ConsulationDoctorBean bean;
    private ConsultantsDialogAdapter adapter;
    private String doctors="";
    @Override
    public int intiLayout() {
        return R.layout.activity_consulation;
    }

    @Override
    public void initView() {
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("会诊");
        et_yy=findViewById(R.id.et_yy);
        et_yj=findViewById(R.id.et_yj);
        rl_hz_doctor=findViewById(R.id.rl_hz_doctor);
        tv_show_doctor= findViewById(R.id.tv_show_doctor);
        tv_huizhen_date=findViewById(R.id.tv_huizhen_date);
        rl_hz_doctor.setOnClickListener(this);
        tv_huizhen_date.setOnClickListener(this);
        rl_hz_ok=findViewById(R.id.rl_hz_ok);
        rl_hz_ok.setOnClickListener(this);
        jmid=getIntent().getIntExtra("jmid",0);
        jzid=getIntent().getIntExtra("jzid",0);
    }

    @Override
    public void initData() {
        getDoctorNetWork();
    }

    /**
     * 上传
     * @param
     * @param yy
     * @param yj
     * @param showdoctor
     */
    private void getNetWork(String yy, String yj, String showdoctor,String date){
        int ysid= (int) SpUtils.get(ConsultationActivity.this,"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("jmId",jmid);
        map.put("hzyx",yy);
        map.put("hzyz",yj);
        map.put("hzys",showdoctor);
        map.put("hzrq",date);
        map.put("jzId",jzid);
        map.put("zrysId",ysid);
        OkHttpUtils.getNetWork(ConsultationActivity.this, Constant.baseurl+"online/jzzz/hz",map,handler);
    }

    /**
     * 获取医生
     */
    private void getDoctorNetWork(){
        int ysid= (int) SpUtils.get(ConsultationActivity.this,"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("ysid",ysid);
        OkHttpUtils.getNetWork(ConsultationActivity.this, Constant.baseurl+"online/user/xtsqysList",map,doctorhandler);
    }
    private Handler doctorhandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    bean= gson.fromJson(s,ConsulationDoctorBean.class);
                    if(bean.getCode()==0){

                    }else{
                        Toast.makeText(ConsultationActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(ConsultationActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(ConsultationActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(ConsultationActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    DataBean bean= gson.fromJson(s,DataBean.class);
                    if(bean.getCode()==0){
                        Toast.makeText(ConsultationActivity.this,"会诊成功",Toast.LENGTH_SHORT).show();
                        finish();
                    }else{
                        Toast.makeText(ConsultationActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(ConsultationActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(ConsultationActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(ConsultationActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    Dialog dialog;
    /**
     * 弹出选择会诊对话框
     */
    private void showConsultantsSignDialog(){
        dialog = new Dialog(this, R.style.dialog);
        View view_dialog = LayoutInflater.from(this).inflate(
                R.layout.consultants_dialog, null);
        TextView tv_consulatsns_ok=view_dialog.findViewById(R.id.tv_consulatsns_ok);
        RecyclerView recyclerView=view_dialog.findViewById(R.id.consultans_recycleview);
        tv_consulatsns_ok.setOnClickListener(this);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        if(bean!=null){
            adapter=new ConsultantsDialogAdapter(this,bean.getData());
            recyclerView.setAdapter(adapter);
            adapter.setSeclectListener(new ConsultantsDialogAdapter.SeclectListener() {
                @Override
                public void select(boolean isCheck,int position) {
                    if(isCheck){
                        Log.e("cccc",bean.getData().get(position).getName());
                        doctors+=bean.getData().get(position).getName()+",";
                    }
                }
            });
        }
        dialog.setContentView(view_dialog);
        Window dialogWindow = dialog.getWindow();
        //设置位置
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog的宽高属性
        dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_hz_doctor:  //弹出选择医生对话框
                showConsultantsSignDialog();
                break;
            case R.id.rl_hz_ok:
                String yy=et_yy.getText().toString().trim();
                if(TextUtils.isEmpty(yy)){
                    Toast.makeText(ConsultationActivity.this,"会诊原因不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                String yj=et_yj.getText().toString().trim();
                if(TextUtils.isEmpty(yj)){
                    Toast.makeText(ConsultationActivity.this,"会诊意见不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                String showdoctor=tv_show_doctor.getText().toString();
                if(TextUtils.isEmpty(showdoctor)||showdoctor.equals("")){
                    Toast.makeText(ConsultationActivity.this,"请选择会诊医生",Toast.LENGTH_SHORT).show();
                    return;
                }
                String date1=tv_huizhen_date.getText().toString();
                if(TextUtils.isEmpty(date1)){
                    Toast.makeText(ConsultationActivity.this,"请选择会诊时间",Toast.LENGTH_SHORT).show();
                    return;
                }
                getNetWork(yy,yj,showdoctor,date1);
                break;
            case R.id.tv_consulatsns_ok:  //弹出选择医生对话框确定
                if(dialog!=null){
                    dialog.dismiss();
                }
                if(doctors.endsWith(",")){
                    doctors=doctors.substring(0,doctors.length()-1);
                }
                tv_show_doctor.setText(doctors);
                doctors="";
                break;
            case R.id.tv_huizhen_date:  //会诊日期
                Util.alertTimerPicker(this, TimePickerView.Type.YEAR_MONTH_DAY,"yyyy-MM-dd", date -> {
                    tv_huizhen_date.setText(date);
                });
                break;
        }
    }
}
